package com.yr.ryjk.ads;

import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.ads.entity.Ads;
import com.yr.ryjk.ads.service.IAdsService;
import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.util.JSONUtil;

/**
 * 广告
 * @author hxjxiaojun
 */
@For(user=UserType.WEBSERVER_USER)
@Controller
@RequestMapping("/ads")
public class AdsController {
	@Autowired
	private IAdsService adsService;

	/**
	 * 获取指定广告的详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDepartmentInfo(String id) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,adsService.getAdsById(id));
	}

	/**
	 * 添加广告
	 * @param ads
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Ads ads) {
		adsService.add(ads);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	/**
	 * 删除广告
	 * @param id 记录id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("id") @NotBlank String id) {
		String[] classList =id.split(",");
		adsService.delete(classList);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	/**
	 * 修改广告
	 * @param id 记录id
	 * @param type 类型
	 * @param picUrl  图片url
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@RequestParam("id") @NotBlank String id,
			@RequestParam(value="type",defaultValue="banner") @NotBlank String type,
			@RequestParam("picUrl") @NotBlank String picUrl,
			@RequestParam("externalUrl") @NotBlank String externalUrl) {
		adsService.modify(id, type, picUrl, externalUrl);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	/**
	 * 分页获取数据
	 * @param pageNo 页码
	 * @param pageSize   每页数量
	 * @param type  广告类型
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
			@RequestParam(value="type",defaultValue="banner") @NotBlank String type) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,
				adsService.getListOfPage(pageNo, pageSize, type));
	}

	/**
	 * app端分类型获取列表
	 * @param type
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getList(
			@RequestParam(value="type",defaultValue="banner") @NotBlank String type) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,adsService.getListOfType(type));
	}

	/**
	 * 刷新时间
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/refresh", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> refreshTime(
			@RequestParam("id") @NotBlank String id) {
		adsService.updateRefreshTime(id);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 浏览图片
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/adsPicture", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> adsPicture(
			@RequestParam(value="type",defaultValue="banner") @NotBlank String type) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,adsService.getListOfType(type));
	}
}